<template>
  <el-form :model="item" ref="moForm" :rules="rules" label-width="130px">
    <el-form-item prop="url" label="链接">
      <el-input v-model.trim="item.url" placeholder="请输入链接"></el-input>
    </el-form-item>
    <el-form-item prop="schoolId" label="所属院校">
      <el-select v-model="item.schoolId" placeholder="请选择所属院校">
        <el-option
          v-for="it in schoolList"
          :key="it.id"
          :label="it.name"
          :value="it.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="imageFile" label="图片">
      <el-button class="setimg" type="primary" @click="setImg"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过1M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput"
        @change="imgChange($event)"
      />
      <div class="imgbox">
        <img :src="iconImage" id="image" />
      </div>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="handleSubmitClick()">提交</el-button>
      <el-button @click="handleBackClick">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSchoolList } from "./api";
import {filterSchool} from '../../../utils/common'
export default {
  props: ["item"],
  data() {
    return {
      schoolList: [],

      reader: null,
      imgBase: "",
      iconImage: "",
      rules: {
        url: [{ required: true, message: "请输入链接", trigger: "blur" }],
        schoolId: [
          { required: true, message: "请选择院校", trigger: "change" },
        ],
        imageFile: [{ required: true, message: "请选择图片", trigger: "blur" }],
      },
    };
  },
  computed: {
    obj() {
      return {
        url: this.item.url,
        schoolId: this.item.schoolId,
        image: this.item.image,
        imageFile: this.item.imageFile,
      };
    },
  },
  watch: {},
  created() {
    this.refresh_data();
    this.reader = new FileReader();
    this.reader.onload = (e) => {
      this.imgBase = e.target.result;
    };
    setTimeout(() => {
      if (this.item.image) {
        this.iconImage = this.item.image;
      }
    }, 500);
  },
  methods: {
    // 获取院校列表
    refresh_data() {
      getSchoolList()
        .then((data) => {
          console.log(data, "=====");
          if (data.code === 1) {
            this.schoolList = filterSchool(data.data);
          } else {
            this.$message.error(data.description);
          }
          this.loadList = false;
        })
        .catch((err) => {
          console.log(err);
          this.loadList = false;
        });
    },
    handleBackClick() {
      this.$router.go(-1);
    },
    handleSubmitClick() {
      this.$emit("doSubmit");
    },
    valid(callback) {
      this.$refs["moForm"].validate((valid) => {
        callback(valid);
      });
    },
    setImg() {
      const target = document.getElementById("fileInput");
      target && target.click();
    },
    imgChange(e) {
      console.log(e);
      const img = e.target.files[0];
      if (img.size / 1024 < 2 * 1024) {
        this.reader.readAsDataURL(img);
        this.item.imageFile = img;
        setTimeout(() => {
          const pos = this.imgBase.indexOf("base64") + 7;
          this.imgBase = this.imgBase.substr(pos);
          this.iconImage = "data:image/png;base64," + this.imgBase;
        }, 500);
      } else {
        this.$message.error("图片大小不可超过2M");
      }
      this.$refs["moForm"].validateField("imageFile");
    },
  },
};
</script>

<style scoped>
#fileInput {
  width: 0;
  height: 0;
  overflow: hidden;
}

.setimg {
  float: left;
  margin-left: 0;
}

.imgbox {
  border: 1px solid #eee;
  width: 200px;
  height: auto;
}

.imgbox img {
  align: center;
  max-width: 100%;
}

.imginfo {
  font-size: 12px;
  color: #666;
}
</style>
